﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Airborne.ScrumPoker.Model.TeamFoundation;
using Airborne.ScrumPoker.Module.Events;
using Airborne.ScrumPoker.Module.TeamFoundation.Model;

namespace Airborne.ScrumPoker.Module.TeamFoundation.Views
{
    /// <summary>
    /// Interaction logic for WorkItemSelectView.xaml
    /// </summary>
    public partial class WorkItemSelectView : UserControl, IWorkItemSelectView
    {
        public WorkItemSelectView(TeamFoundationConfiguration configuration)
        {

            InitializeComponent();
            this.Configuration = configuration;
        }

        #region IWorkItemSelectView Members

        public TeamFoundationConfiguration Configuration
        {
            get
            {

                return this.DataContext as TeamFoundationConfiguration;
            }
            set
            {
                this.DataContext = value;
            }
        }

        #region WorkItemSelected

        public event EventHandler<ItemSelectedEventArgs<ITeamWorkItem>> WorkItemSelected;

        private void RaiseWorkItemSelected(ITeamWorkItem workItem)
        {
            ItemSelectedEventArgs<ITeamWorkItem> args = new ItemSelectedEventArgs<ITeamWorkItem>(workItem);
            OnWorkItemSelected(args);
        }

        protected virtual void OnWorkItemSelected(ItemSelectedEventArgs<ITeamWorkItem> args)
        {
            EventHandler<ItemSelectedEventArgs<ITeamWorkItem>> handler = this.WorkItemSelected;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        #region ProjectSelected

        public event EventHandler<ItemSelectedEventArgs<ITeamProject>> ProjectSelected;

        private void RaiseProjectSelected(ITeamProject project)
        {
            ItemSelectedEventArgs<ITeamProject> args = new ItemSelectedEventArgs<ITeamProject>(project);
            OnProjectSelected(args);
        }

        protected virtual void OnProjectSelected(ItemSelectedEventArgs<ITeamProject> args)
        {
            EventHandler<ItemSelectedEventArgs<ITeamProject>> handler = this.ProjectSelected;
            if (handler != null)
            {
                handler(this, args);
            }
        }

        #endregion

        public void SetProjects(ITeamProject[] projects)
        {
            comboBoxProjects.ItemsSource = projects;
            comboBoxProjects.DisplayMemberPath = "Name";
        }

        public void SetWorkItems(ITeamWorkItem[] workItems)
        {
            listBoxWorkItems.ItemsSource = workItems;
        }

        #endregion

        private void comboBoxProjectsSelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            ITeamProject project = comboBoxProjects.SelectedItem as ITeamProject;
            if (project.IsNotNull())
            {
                RaiseProjectSelected(project);
            }
        }

        private void buttonConnect(object sender, RoutedEventArgs e)
        {
            Cursor beforeCursor = Cursor;
            try
            {
                Cursor = Cursors.Wait;
                TeamFoundationCommands.ConnectToServerCommand.Execute(null);
            }
            finally
            {
                Cursor = beforeCursor;
            }
        }

        private void listBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ITeamWorkItem backLogItem = listBoxWorkItems.SelectedItem as ITeamWorkItem;
            if (backLogItem.IsNotNull())
            {
                RaiseWorkItemSelected(backLogItem);
            }
        }


        public void ForceNextWorkItemSelection(int steps)
        {
            if (ForcedSelectionIsValid(steps))
            {
                this.listBoxWorkItems.SelectedIndex = this.listBoxWorkItems.SelectedIndex + steps;
            }
        }

        private bool ForcedSelectionIsValid(int steps)
        {
            var requestedIndex = this.listBoxWorkItems.SelectedIndex + steps;
            var maxLimit = this.listBoxWorkItems.Items.Count -1;
            var minLimit = 0;

            return (requestedIndex <= maxLimit && requestedIndex >= minLimit);
        }
    
    }
}
